package com.jbm.jbmsupplier.zp.adapters;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.zp.entity.Recent_ContactsData;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by donar_work on 2017/5/4.
 */

public class Recent_ContactsAdapter extends BaseAdapter {
    ArrayList<Recent_ContactsData>     list2 ;
    private int to_id;
    Context context;
    public Recent_ContactsAdapter(Context context,ArrayList<Recent_ContactsData> list){
        this.context = context;
        this.list2 = list;
    }

    @Override
    public int getCount() {
        return list2.size();
    }

    @Override
    public Object getItem(int position) {
        return list2.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null){

            convertView = View.inflate(context, R.layout.recent_contacts_text,null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
try {
    initviewdate(viewHolder, list2.get(position),position);
}catch (Exception e){
    Log.e("main","消息页面适配器错误"+e+"1231786");
}

        return convertView;
    }



    private void initviewdate(ViewHolder viewHolder, Recent_ContactsData polis, int position) {
        try {
            viewHolder.tv_last_msgtime.setText(polis.getTime());
            to_id = polis.getTo_id();
            if (polis.getTag()!=1){
                viewHolder.t_name.setText(polis.getName());
                viewHolder.t_MSG.setText(polis.getMsg());
                if (polis.getHead_portrait().equals("")||polis.getHead_portrait().equals("null")){
                viewHolder.nameimage.setImageDrawable(context.getResources().getDrawable(R.mipmap.pic));
            }else{
                ImageOptions i=new ImageOptions.Builder().setCircular(true).setFailureDrawable(context.getResources().getDrawable(R.mipmap.pic)).build();
                x.image().bind(viewHolder.nameimage,polis.getHead_portrait(),i);
            }
                viewHolder.tv_unread_msg.setText(polis.getUnreadMsg()+"");
            }else{
                if (polis.getHead_portrait().equals("1")){
                    viewHolder.nameimage.setImageDrawable(context.getResources().getDrawable(R.mipmap.pic));
                    viewHolder.t_name.setText(polis.getName());
                    viewHolder.t_MSG.setText(polis.getMsg());
                }
                viewHolder.tv_unread_msg.setText(polis.getUnreadMsg()+"");

            }
            ;  if(polis.getUnreadMsg() == 0){
                viewHolder.tv_unread_msg.setVisibility(View.INVISIBLE);
            }else {
                viewHolder.tv_unread_msg.setVisibility(View.VISIBLE);
            }
        }catch (Exception e){
            Log.e("main","消息页面适配器错误"+e);
        }
    }

    public static class ViewHolder {
        TextView t_name;
        TextView t_MSG;
        TextView tv_last_msgtime;
        TextView tv_unread_msg;
        ImageView nameimage;

        public ViewHolder(View convertView ) {
            t_name = (TextView) convertView.findViewById(R.id.t_name);
           t_MSG = (TextView) convertView.findViewById(R.id.t_MSG);
           tv_last_msgtime = (TextView) convertView.findViewById(R.id.tv_last_msgtime);
           tv_unread_msg = (TextView) convertView.findViewById(R.id.tv_unread_msg);
            nameimage = (ImageView) convertView.findViewById(R.id.nameimage);
        }
    }
    public void notifyData(ArrayList<Recent_ContactsData> data){
        list2 = data;
        notifyDataSetChanged();
    }
}
